# -*- coding: utf-8 -*-



## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------


from qtcommon import connect
from PythonQt import QtCore,QtGui

# using a trick to build dynamically Component class
# and a ComponentList class

# the base Componen
class Component(object):
    mainWindow=None

# the list of functions we want to generalize in ComponentList
componentFunctions = [
    'wineRowSelected',
    'countryRowSelected',
    'regionRowSelected',        
    'setup',
    'connect',
    'retranslate'
    ]

# Add them to the base class :
for f in componentFunctions:
    setattr(Component, f, lambda self:None )

def loopFuncBuilder(m):
    """builds a function that call the method named 'm'
    of each element of self """
    def f(self,*args):
        for c in self:
            getattr(c, m) (*args )            
    return f

def listTypeGenerator( klass, generalizedMethods):
    """ This builds a class inheriting from list, defining all the function
    in generalizedMethods thanks to loopFuncBuilder.
    if f is in generalizedMethods, l = klass(), then
      l.f()
    is equivalent to
      for o in l : o.f()
    """
    generalizedType = type(klass.__name__+'List',(list,) ,{})
    for m in generalizedMethods:        
        setattr(generalizedType, m, loopFuncBuilder(m))
    return generalizedType

# Build the ComponentList
ComponentList = listTypeGenerator(Component, componentFunctions )

